package jp.co.nes.awssupport.business.logic.description;

import jp.co.nes.awssupport.business.bean.BillingCsvBean;
import jp.co.nes.awssupport.common.bean.ReservedManageBean;

/**
 * RelationalDatabaseService用の説明作成です。
 */
public class AmazonRelationalDatabaseServiceDescriptionImpl extends BaseDescriptionImpl {

    /**
     * デフォルトコンストラクタです。
     */
    public AmazonRelationalDatabaseServiceDescriptionImpl() {
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.business.logic.description.BaseDescriptionImpl#createDescriptionForReserved(jp.co.nes.awssupport.business.bean.BillingCsvBean, jp.co.nes.awssupport.common.bean.ReservedManageBean)
     */
    @Override
    protected String createDescriptionForReserved(BillingCsvBean csvBean, ReservedManageBean rmBean) {
        String result = getDefaultDescriptionValue(csvBean);

        String productName = csvBean.getProductName();

        String regionCode = rmBean.getRegionCode();

        String reservedInstanceValue = getReservedInstanceValue(productName);
        if (reservedInstanceValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String operationValue = getOperationValueForReserved(productName, rmBean);
        if (operationValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String multiAZOrSingleAZValue = null;
        if ("0".equals(rmBean.getMultiAZ())) {
            multiAZOrSingleAZValue = descriptionInfoHelper.get(productName, "Single-AZ");
        } else if ("1".equals(rmBean.getMultiAZ())) {
            multiAZOrSingleAZValue = descriptionInfoHelper.get(productName, "Multi-AZ");
        } else {
            addWarnMessage(csvBean);
            return result;
        }
        if (multiAZOrSingleAZValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String instanceType = rmBean.getInstanceType();
        if (instanceType == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String durationValue = descriptionInfoHelper.get(productName, Integer.toString(rmBean.getDuration()));
        if (durationValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String offeringTypeValue = descriptionInfoHelper.get(productName, rmBean.getOfferingType());
        if (offeringTypeValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String lineItemTypeValue = getLineItemTypeValueForReserved(productName, csvBean.getLineItemType());
        if (lineItemTypeValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        StringBuilder builder = new StringBuilder();
        builder.append(getDescriptionPrefix(regionCode));
        builder.append(reservedInstanceValue);
        builder.append(FWC).append(operationValue);
        builder.append(FWC).append(multiAZOrSingleAZValue);
        builder.append(FWC).append(instanceType);
        builder.append(FWC).append(durationValue);
        builder.append(FWC).append(offeringTypeValue);
        builder.append(FWC).append(lineItemTypeValue);
        result = builder.toString();
        return result;
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.business.logic.description.BaseDescriptionImpl#buildDescriptionFromUsageType(java.lang.String, java.lang.String, jp.co.nes.awssupport.business.logic.description.BaseDescriptionImpl.UsageTypeTokenizer)
     */
    @Override
    protected String buildDescriptionFromUsageType(String regionCode, String productName,
            UsageTypeTokenizer usageTypeTokenizer) {
        String kindValue = descriptionInfoHelper.get(productName, "OnDemandInstance");
        String instanceType = usageTypeTokenizer.getInstanceType();
        String operation = usageTypeTokenizer.getOperation();
        String operationValue = descriptionInfoHelper.get(productName, operation);
        if (operationValue == null && operation != null) {
            return null;
        }
        String deploymentOption = usageTypeTokenizer.getKind();
        String deploymentOptionValue = descriptionInfoHelper.get(productName, deploymentOption);
        if (deploymentOptionValue == null && deploymentOption != null) {
            return null;
        }

        StringBuilder builder = new StringBuilder();
        if (kindValue != null && instanceType != null) {
            builder.append(getDescriptionPrefix(regionCode));
            builder.append(kindValue);
            if (operationValue != null) {
                builder.append(FWC).append(operationValue);
            }
            if (deploymentOptionValue != null) {
                builder.append(FWC).append(deploymentOptionValue);
            }
            builder.append(FWC).append(instanceType);
        }
        return builder.toString();
    }
}
